<?php
declare (strict_types=1);

namespace app\admin\logic\site\navi;

use app\admin\logic\system\Log;
use app\admin\model\site\navi\NaviTop as NaviTopModel;
use app\common\exception\LogicException;

/**
 * 头部导航配置逻辑层
 */
class NaviTop extends NaviTopModel
{
    /**
     * 获取列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     *
     * @return   [type]                         [description]
     */
    public function getList(array $map = [], int $pid = 0)
    {
        $list = $this->where('pid', $pid)->where($map)->order(['sort' => 'asc', 'update_time' => 'desc'])->select();
        if ($list) {
            foreach ($list as $navi) {
                $navi->hasChildren = $this->hasChildren($navi->id);
            }
        }

        return $list;
    }

    /**
     * 是否有子集
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     */
    public function hasChildren(int $pid = 0): bool
    {
        return $this->where('pid', $pid)->find() ? true : false;
    }

    /**
     * 创建导航
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     *
     * @param array $data [description]
     * @return   [type]                               [description]
     */
    public function createNavi(int $pid, array $data)
    {
        $save = [
            'title'     => $data['title'],
            'pid'       => $pid,
            'data_type' => $data['data_type'],
            'content'   => $data['content'],
        ];

        // 验证数据
        $this->getValidate()->scene('create_navi')->failException()->check($save);
        // 创建成功
        if (!$this->save($save)) {
            throw new LogicException('创建失败');
        }

        // 创建成功
        Log::write('新建头部导航【' . $data['title'] . '】', 'top_navi', 'create', true);

        // 返回值
        return $this;
    }

    /**
     * 编辑分组
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     *
     * @param    [type]                         $id   [description]
     * @param    [type]                         $data [description]
     * @return   [type]                               [description]
     */
    public function editNavi(int $id, array $data)
    {
        $navi = $this->where('id', $id)->findOrFail();
        $save = [
            'id'        => $id,
            'title'     => $data['title'],
            'data_type' => $data['data_type'],
            'content'   => $data['content'],
        ];
        // 验证数据
        $this->getValidate()->scene('edit_navi')->failException()->check($save);

        $navi->title     = $save['title'];
        $navi->data_type = $save['data_type'];
        $navi->content   = $save['content'];

        if (!$navi->save()) {
            throw new LogicException('修改失败');
        }

        // 更新成功
        Log::write('更新头部导航【' . $save['title'] . '】', 'top_navi', 'update', true);

        return $navi;
    }

    /**
     * 删除
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-28
     *
     * @param    [type]                         $id [description]
     * @return   [type]                             [description]
     */
    public function deleteNavi(int $id)
    {
        $navi = $this->where('id', $id)->findOrFail();

        $title = $navi->getAttr('title');

        if ($this->hasChildren($navi->id)) {
            throw new LogicException("当前分组下存在导航，不允许删除");
        }

        if (!$navi->delete()) {
            throw new LogicException("删除失败");
        }

        // 更新成功
        Log::write('删除头部导航【' . $title . '】', 'top_navi', 'delete', true);
    }

    /**
     * 更新头部导航顺序
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/10/25
     *
     * @param int $id
     * @param int $sort
     */
    public function updateSort(int $id, int $sort)
    {
        $navi       = $this->where('id', $id)->findOrFail();
        $navi->sort = $sort;
        if (!$navi->force()->save()) {
            throw new LogicException("更新失败");
        }

        // 更新成功
        Log::write('更新头部导航【' . $navi->title . '】顺序', 'top_navi', 'update', true);
    }
}
